//
//  LMRightDatePicker.m
//  LMTowDatePicker
//
//  Created by 天掌科技 on 2018/1/8.
//  Copyright © 2018年 天掌科技. All rights reserved.
//

#import "LMRightDatePicker.h"
#import "Constant.h"

@interface LMRightDatePicker ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic,strong) NSArray *arr;
@end

@implementation LMRightDatePicker
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, Height(720/2))];
        _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        _datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CHS_CN"];
        
        [_datePicker addTarget:self action:@selector(handleDate:) forControlEvents:UIControlEventValueChanged];
        _datePicker.backgroundColor = [UIColor whiteColor];
        //        [self addSubview:_datePicker];
        self.arr = @[@"上午",@"下午"];
        [self addSubview:self.picker];
    }
    
    return self;
}
-(UIPickerView *)picker {
    if (!_picker) {
        _picker =[[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, Height(720/2))];
        _picker.delegate = self;
        _picker.dataSource = self;
        _picker.backgroundColor = [UIColor whiteColor];
        
    }
    return _picker;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return self.arr[row];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.arr.count;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *currentStr = self.arr[row];
    if (self.timerBlock) {
        self.timerBlock(currentStr);
    }
}
/**
 选择完成后
 
 @param datePicker datePicker
 */
//- (void)handleDate:(UIDatePicker *)datePicker
//{
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:@"HH:mm"];
//    NSString *currentDateString = [dateFormatter stringFromDate:datePicker.date];
//    NSLog(@"currentDateString:%@",currentDateString);
//    if (self.timerBlock) {
//        self.timerBlock(currentDateString);
//    }
//}
//
//
//- (void)setTimerStr:(NSString *)timerStr
//{
//    _timerStr = timerStr;
//    NSLog(@"timerStr:%@",timerStr);
//    if (_timerStr) {
//        NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
//
//        [dateFormat setDateFormat:@"HH:mm"];//设定时间格式,这里可以设置成自己需要的格式
//
//        _datePicker.date = [dateFormat dateFromString:_timerStr];
//    }
//}
/**
 选择完成后
 
 @param datePicker datePicker
 */
- (void)handleDate:(UIDatePicker *)datePicker
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.AMSymbol = @"上午";
    dateFormatter.PMSymbol = @"下午";
    //    [dateFormatter setDateFormat:@"HH:mm"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd-aa-HH:mm"];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSString *currentDateString = [dateFormatter stringFromDate:datePicker.date];
    NSString *currentStr = [currentDateString substringWithRange:NSMakeRange(11,2)];
//    NSLog(@"currentDateString:%@",currentStr);
    if (self.timerBlock) {
        self.timerBlock(currentStr);
    }
}


- (void)setTimerStr:(NSString *)timerStr
{
    _timerStr = timerStr;
//    NSLog(@"timerStr:%@",timerStr);
    if (_timerStr) {
        NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
        
        [dateFormat setDateFormat:@"HH:mm"];//设定时间格式,这里可以设置成自己需要的格式
        
        _datePicker.date = [dateFormat dateFromString:_timerStr];
    }
}

@end
